package com.ncp.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ncp.model.biz.product.ManageFrontProductBiz;
import com.ncp.model.dao.DB;
import com.ncp.model.entity.product.ProductAllInfoBean;
import com.ncp.model.entity.product.ProductSortBean;
import com.ncp.model.entity.product.ProductUpdateBean;

public class ManagerUpdateServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		DB db = new DB();
		if(type.equals("ProductSort")==false){
		int ProductID = Integer.parseInt(request.getParameter("ProductID"));
		
		db.setSql("select "+type+" from PRODUCTBASIC where ProductID="+ProductID);
		String value = db.getProductItem();
		
		ProductUpdateBean pub = new ProductUpdateBean();
		pub.setProductID(ProductID);
		pub.setType(type);
		pub.setValue(value);
		
		if(type.equals("ProductSortID")){
			
			ManageFrontProductBiz mbp = new ManageFrontProductBiz();
		    List<ProductSortBean> listparent =  mbp.getProductSort("select * from PRODUCTSORT where isover=0");
		    String ProductSortName = request.getParameter("ProductSortName");
			pub.setProductSortName(ProductSortName);
			
			request.setAttribute("listparent", listparent);
			request.setAttribute("pub", pub);
			request.getRequestDispatcher("pages/admin/product/updateProductSortName.jsp").forward(request,response);

		}
		else if(type.equals("ProductImage")){
			
			request.setAttribute("pub", pub);
			request.getRequestDispatcher("pages/admin/product/updateProductImage.jsp").forward(request,response);
			
		}
		
        else if(type.equals("ProductDescription")){
        	request.setAttribute("pub", pub);
			request.getRequestDispatcher("pages/admin/product/updateProductDescription.jsp").forward(request,response);
			
			
		}
		
		 else {
			request.setAttribute("pub", pub);
        	request.getRequestDispatcher("pages/admin/product/updateItems.jsp").forward(request,response);
        }
		}
		
		else if(type.equals("ProductSort")){
			int ProductSortID = Integer.parseInt(request.getParameter("ProductID"));
			db.setSql("select ProductSortName from PRODUCTSORT where ProductSortID="+ProductSortID);
			String value = db.getProductItem();
			String type1 = "ProductSortFromSort";
			ProductUpdateBean pub = new ProductUpdateBean();
			pub.setValue(value);
			pub.setProductID(ProductSortID);
			pub.setType(type1);
			request.setAttribute("pub", pub);
			request.getRequestDispatcher("pages/admin/product/updateItems.jsp").forward(request,response);
			
			
		}
	}
	

}
